package com.kgc.manger.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.TbContentCategoryMapper;
import com.kgc.dao.TbContentMapper;
import com.kgc.entity.TbContent;
import com.kgc.entity.TbContentExample;
import com.kgc.manger.jedis.JedisClient;
import com.kgc.manger.service.ContentService;
import com.kgc.manger.utils.JsonUtils;
import com.kgc.utils.Ad;
import com.kgc.utils.ItripResult;
import com.kgc.utils.ResultTable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper contentMapper;

    @Value("${ad1}")
    private String ad1;

    @Autowired
    private JedisClient jedisClient;
    @Override
    public ResultTable findAll(int page, int limit) {
        PageHelper.startPage(page,limit);
        TbContentExample example=new TbContentExample();
        PageInfo<TbContent> pageInfo=new PageInfo(contentMapper.selectByExample(example));
        ResultTable resultTable=new ResultTable();
        resultTable.setCode(0);
        resultTable.setCount(pageInfo.getTotal());
        resultTable.setData(pageInfo.getList());
        resultTable.setMsg("ok");
        return resultTable;
    }

    @Override
    public ItripResult addContent(TbContent content) {
        contentMapper.insert(content);
        return ItripResult.ok();
    }

    //根据内容编号来查询所有的内容 89  大广告
    @Override
    public List<Ad> findAllAd(Long cid) {
        String jsonString="";
        List<TbContent> list=null;
        //1.判断key=ad1 在redis中是否存在
        if(jedisClient.exists(ad1)){//为true表示redis中存放缓存的内容
            jsonString=jedisClient.get(ad1);//从redis中取缓的json对象
           // JsonUtils.jsonToPojo(jsonString,TbContent.class);
           list= JsonUtils.jsonToList(jsonString,TbContent.class);
        }else{//不存在
            System.out.println("开始访问数据库.....");
            TbContentExample example=new TbContentExample();
            TbContentExample.Criteria criteria = example.createCriteria();
            criteria.andCategoryIdEqualTo(cid);
            list= contentMapper.selectByExample(example);
            System.out.println("访问数据库结束.....");
            //将List集合转换成json字符串
            jsonString=JsonUtils.objectToJson(list);
            jedisClient.set(ad1,jsonString);
            jedisClient.expire("ad1",60*60*24);
        }

        //2.如果ad1存在  从redis中大广告信息 json  转换成List

        //3.如果不存在，查询数据库再将集合转换成json字符串，缓存到redis




        List<Ad> listAd=new ArrayList<>();
        for(TbContent content:list){
            Ad ad=new Ad();
            ad.setHref(content.getUrl());
            ad.setSrc(content.getPic());
            ad.setSrcB(content.getPic2());
            ad.setAlt(content.getSubTitle());
            listAd.add(ad);
        }
        return listAd;
    }
}
